<template>
  <el-dialog id="edit-dialog" :title="title" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false" width="60%">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('msgCenter.bulletin.theme')" prop="subject">
        <el-input v-model="formValidate.subject" :disabled="state === 'view'" :placeholder="$t('msgCenter.bulletin.prompt.theme')" :maxlength="50" />
      </el-form-item>
      <el-form-item :label="$t('msgCenter.bulletin.content')" prop="pushContent">
        <el-input
          v-model="formValidate.pushContent"
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          :placeholder="$t('msgCenter.bulletin.prompt.content')"
          :maxlength="500"
          :disabled="state === 'view'"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('msgCenter.bulletin.showTime')" prop="time">
        <el-date-picker
          v-model="time"
          type="datetimerange"
          range-separator="~"
          :start-placeholder="$t('common.startDate')"
          :end-placeholder="$t('common.endDate')"
          :disabled="state === 'view'"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item :label="$t('msgCenter.bulletin.pushObj')" prop="pushObj">
        <el-select v-model="formValidate.pushObj" clearable :disabled="state === 'view'" :placeholder="$t('msgCenter.bulletin.prompt.pushObj')" style="width: 100%">
          <el-option
            v-for="item in $t('msgCenter.bulletin.pushObjType')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button v-if="state !== 'view'" type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateMsgPush, addMsgPush } from '@/api/operation'
import { formatDate } from '@/utils/method'
export default {
  name: 'BulletinDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    title: {
      type: String,
      default: null
    },
    state: {
      type: String,
      default: null
    }
  },
  data() {
    const validateTime = (rule, value, callback) => {
      if (this.time && this.time.length > 1) {
        callback()
      } else {
        callback(new Error(this.$t('msgCenter.bulletin.showTime') + this.$t('common.null')))
      }
    }
    return {
      submitLoading: false,
      time: null,
      formValidate: {
        id: null,
        subject: null,
        pushTime: null,
        pushTimeEnd: null,
        pushType: null,
        pushContent: null,
        pushObj: null,
        isNotice: null
      },
      rules: {
        subject: [{ required: true, message: this.$t('msgCenter.bulletin.theme') + this.$t('common.null'), trigger: 'blur' }],
        pushContent: [{ required: true, message: this.$t('msgCenter.bulletin.content') + this.$t('common.null'), trigger: 'blur' }],
        time: [{ required: true, validator: validateTime, trigger: 'blur' }],
        pushObj: [{ required: true, message: this.$t('msgCenter.bulletin.pushObj') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    setTime(start, end) {
      this.time = [start, end]
    },
    handleClose() {
      this.$emit('change-dialog', false)
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          data['pushTime'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
          data['pushTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
          const postData = new FormData()
          for (const key in this.formValidate) {
            postData.append(key, data[key])
          }
          if (data['id']) {
            updateMsgPush(postData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addMsgPush(postData)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
